<?php

/**
 * 国际快递悠奇报价 
 */
class Application_Model_ExpressPrice extends Application_Model_DomainObject {
    
    protected $_id;
    
    protected $_expressId;//快递ID
    
    protected $_cityId;//城市ID

    protected $_firstWeight;//首重
    
    protected $_subsequentWeight;//续重
    
    protected $_firstPrice;//首重价(含燃油)
    
    protected $_subsequentPrice;//续重价(含燃油)
    
    protected $_price5;//5KG以上续重价

    protected $_price10;//10KG以上续重价
    
    protected $_price20;//20KG以上续重价
    
    protected $_price40;//40KG以上续重价
    
    protected $_price70;//70KG以上续重价

    protected $_firstFuel;//首重燃油费（只是记录下）
    
    protected $_subsequentFuel;//续重燃油费（只是记录下）
    
    protected $_typeMark;//货物类型掩码 如接收些类型，进行“与”运算后为真,如果为NULL，则取Express的值
    
    protected $_times;//时效

    protected $_firstReport;//官方首重报价
    
    protected $_subsequentReport;//官方续重报价

    protected $_isDisable;//是否禁用
    
    protected $_sortKey;


    /**
     * 排序专用字段
     * @var type 
     */
    public $SortKey;

    private $_city;
    public function getCity(){
        if($this->_city == NULL)
            $this->_city == Application_Model_city::load ($this->_cityId);
        
        return $this->_city;
    }

    /**
     * 返回对应的快递公司
     * @return Application_Model_Express 
     */
    public function getExpress(){
        return Application_Model_Express::load($this->_expressId);
    }

    /**
     * 判断该物流是否接收此类型的货物
     * @param int $type 货物类型
     * @param Application_Model_ExpressPrice $expressPrice
     * @return bool 
     */
    public function isAccept($type){
        return $this->getExpress()->isAccept($type,$this);
    }
    
    /**
     * 根据城市ID返回对应的快递报价
     * @param int $cityId 
     * @return array
     */
    public static function getPricesByCity($cityId){
        return Application_Model_ExpressPrice::getDao()->queryAll(array('cityId'=>$cityId));
    }
    
    /**
     * 根据重量计算价格
     * @param type $weight 重量
     * @return type  价格
     */
    public function getMoneyByweight($weight){
        if($this->_firstWeight>=$weight){
            return $this->_firstPrice;
        }
        
        $mod = ($weight-($this->_firstWeight)) % $this->_subsequentWeight;//余数
        $baishu = ($weight - $this->_firstWeight - $mod) / $this->_subsequentWeight + ($mod>0?1:0); // 倍数
        $weight = $this->_firstWeight + ($baishu* $this->_subsequentWeight);
        
        
        $price = $this->_subsequentPrice;
        if($weight>=71000){
            $price = $this->_price70;
        }else if($weight>=41000){
            $price = $this->_price40;
        }else if($weight>=21000){
            $price = $this->_price20;
        }else if($weight>=11000){
            $price = $this->_price10;
        }else if($weight>=6000){
            $price = $this->_price5;
        }
        
        $menoy = $this->_firstPrice + $baishu *$price;
        return $menoy;        
    }
    
    /**
     * 根据重量，返回官方报价
     * @param type $weight
     * @return type 
     */
    public function getReportMoney($weight){
        if($this->_firstWeight>=$weight){
            return $this->_firstReport;
        }
        
        $mod = ($weight-($this->_firstWeight)) % $this->_subsequentWeight;//余数
        $baishu = ($weight - $this->_firstWeight - $mod) / $this->_subsequentWeight + ($mod>0?1:0); // 倍数
        $menoy = $this->_firstReport + $baishu *$this->_subsequentReport;
        return $menoy;             
    }
    
    /**
     * 根据快递公司ID一次取起该快递的所有报价
     * @param type $expressId
     * @return array 
     */
    public static function getPricesByExpress($expressId){
        return Application_Model_ExpressPrice::getDao()->queryAll(array('expressId'=>$expressId));
    }
    
    /**
     * 根据快递公司ID与城市ID，取出对像的报价
     * @param type $expressId
     * @param type $cityId
     * @return Application_Model_ExpressPrice 
     */
    public static function getPriceByExpressAndCity($expressId,$cityId){
        return Application_Model_ExpressPrice::getDao()->queryOne(array('expressId'=>$expressId,'cityId'=>$cityId));
    }
}

